package com.china08.yxyapi.api.home.activity.weike;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.huodong.YxyHuodongWeikeContestCatalogue;
import com.china08.yxyapi.entity.db.res.YxyResBook;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyHuodongWeikeContestCatalogueRepository;
import com.china08.yxyapi.repository.db.YxyResBookRepository;
import com.china08.yxyapi.security.profile.YxyProfile;

@CrossOrigin
@RestController
public class ActivityApplyController extends YxyApiBaseController {

    @Autowired
    private YxyResBookRepository yxyResBookRepository;

    @Autowired
    private YxyHuodongWeikeContestCatalogueRepository catalogueRepository;

    @RequestMapping(value = "/api_yw/home/activity/tea/apply", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(@RequestBody ReqModel reqmodel, //
            HttpServletRequest request, //
            HttpServletResponse response//
    ) {
        YxyProfile profile = getProfile(request, response);
        String userId = profile.getId();
        YxyResBook yxyResBook = yxyResBookRepository.findById(reqmodel.bookId);
        List<String> knowId = new ArrayList<String>();
        List<String> knowName = new ArrayList<String>();
        String[] temp = null;
        for (String string : reqmodel.checkList) {
            temp = string.split("-");
            knowId.add(temp[0]);
            knowName.add(temp[1]);
        }
        YxyHuodongWeikeContestCatalogue catlog = new YxyHuodongWeikeContestCatalogue();
        catlog.setUserId(userId);
        catlog.setPhaseId(yxyResBook.getPhaseId());
        catlog.setPhaseName(yxyResBook.getPhaseName());
        catlog.setSubjectId(yxyResBook.getSubjectId());
        catlog.setSubjectName(yxyResBook.getSubjectName());
        catlog.setVersionId(yxyResBook.getVersionId());
        catlog.setVersionName(yxyResBook.getVersionName());
        catlog.setSemesterId(yxyResBook.getSemersterId());
        catlog.setSemesterName(yxyResBook.getSemersterName());
        catlog.setBookContentId(reqmodel.contentId);
        catlog.setBookContentName(reqmodel.contentName);
        catlog.setKnowIds(String.join(",", knowId));
        catlog.setKnowNames(String.join(",", knowName));
        // catlog.setApply(true);
        catlog = catalogueRepository.save(catlog);
        RespModel respModel = new RespModel();
        respModel.id = catlog.getId();
        respModel.phaseName = catlog.getPhaseName();
        respModel.subjectName = catlog.getSubjectName();
        respModel.versionName = catlog.getVersionName();
        respModel.semesterName = catlog.getSemesterName();
        respModel.catalogName = catlog.getBookContentName();
        respModel.knowledageName = catlog.getKnowNames();
        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {
        public String bookId;
        // 绑定的章节id
        public String contentId;

        // 绑定的章节名称
        public String contentName;

        public String[] checkList;
    }

    protected static class RespModel extends YxyApiRespModel {
        public String id;
        public String phaseName;
        public String subjectName;
        public String versionName;
        public String semesterName;
        public String catalogName;
        public String knowledageName;
    }

}
